package baseclass.e_tree;

/*
给树节点增加一个parent指针。头节点的parent指向null。只给一个在
二叉树中的某个节点 node， 请实现返回node的后继节点的函数。
 在二叉树的中序遍历的序列中， node的下一个节点叫作node的后继节点。
 node的上一个节点叫node的前驱节点.


找后继，就是要判断当前节点是否有右孩子。若有，则其右孩子的最左是。若没有，则寻着父节点找，找到第一个左子树
 1)如果当前节点有右孩子，则其右孩子的最左孩子是当前节点的后继
 2)如果当前节点没有右孩子，则找当前节点的父节点，直到节点是其父节点的左孩子，该父节点就是后继。

 特殊情况，中序遍历的最后一个节点没有后继。
 */
class Node {
    int value;
    Node left;
    Node right;
    Node parent;

    Node(int data) {
        this.value = data;
    }
}

public class Code02_AfterNode_后继节点{

    public static Node getSuccessorNode(Node node){
        if(node == null) return null;
        //如果当前节点有右孩子， 则把权限给右孩子，找右孩子最左最左的那个节点(如果有)
        if(node.right != null) {
            return getLeftestNode(node.right);
        }
        //否则一直往上找节点，该节点满足是 其 父节点  的左孩子
        Node parent = node.parent;
        //如果是root
        if(parent == null) return null;
        while (parent.left != node){
            node = parent;
            parent = node.parent;
            //注意节点可能是中序遍历的最后一个节点，那么一直找到root也不满足。
            if(parent == null) return null;

        }
        return parent;
    }
    private static Node getLeftestNode(Node node){
        //如果没有左孩子，直接返回自己
        while (node.left != null) node = node.left;
        return node;
    }
    public static void main(String[] args) {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        Node test = head.left.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.left.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.left.right.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.left.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.left;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right;
        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
        test = head.right.right; // 10's next is null
        System.out.println(test.value + " next: " + getSuccessorNode(test));
    }


}
